package com.obsidian.controller;

import com.alibaba.fastjson.JSON;
import com.obsidian.service.ISigarService;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.SigarException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/sigar")
public class SigarController {

    private Logger logger = LoggerFactory.getLogger(SigarController.class);

    @Autowired
    private ISigarService sigarService;

    @RequestMapping("/index")
    public String index(Model model) {
        try {
            CpuInfo[] cpuInfoList = sigarService.getCpuInfoList();
            model.addAttribute("cpuInfoList", cpuInfoList);
        } catch (SigarException e) {
            logger.error("获取CpuInfo失败", e);
        }
        return "manager";
    }

    @RequestMapping("/getCpuPerc")
    @ResponseBody
    public CpuPerc[] getCpuPerc() {
        try {
            CpuPerc[] cpuPercList = sigarService.getCpuPercList();
            return cpuPercList;
        } catch (SigarException e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/getMememory")
    @ResponseBody
    public Mem getMememory(){
        try {
            Mem mememory = sigarService.getMememory();
            return mememory;
        } catch (SigarException e) {
            e.printStackTrace();
        }
        return null;
    }


}
